import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:yp/Xwidgets/cache_image.dart';
import 'package:yp/config/config.dart';
import 'package:yp/pages/order_groupsettlement/view.dart';
import 'package:yp/pages/order_settlements/widgets/style.dart';
import 'package:yp/pages/order_settlements/widgets/text_input.dart';

// import 'package:yp/widgets/button_widget.dart';
import 'package:yp/Xwidgets/pay_bottom_sheet.dart';
import 'package:flutter_datetime_picker/flutter_datetime_picker.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:yp/pages/order_settlements/widgets/EsIconButton.dart';
import 'package:yp/router.dart';
import 'package:yp/utils/util.dart';
import 'logic.dart';
import 'state.dart';
import 'package:intl/intl.dart';
import 'package:yp/helper.dart';

typedef ClickCallback = void Function(int selectIndex, String selectString,
    {Map<String, dynamic> info});

class FontSaleOrderPage extends StatefulWidget {
  @override
  _FontSaleOrderPageStates createState() => _FontSaleOrderPageStates();
}

class _FontSaleOrderPageStates extends State<FontSaleOrderPage>
    with SingleTickerProviderStateMixin {
  final FontSaleOrderLogic logic = Get.put(FontSaleOrderLogic());
  final FontSaleOrderState state = Get.find<FontSaleOrderLogic>().state;

  @override
  void initState() {
    super.initState();
  }

  Function setGift(index) {
    logic.clickGift(index);
  }

  @override
  Widget build(
    BuildContext context,
  ) {
    return WillPopScope(
      onWillPop: () async {
        Get.back(result: true);
        return true;
      },
      child: Scaffold(
          appBar: AppBar(
            backgroundColor: Config.mainColor,
            brightness: Brightness.light,
            iconTheme: IconThemeData(color: Colors.white),
            title: Text(
              "结算",
              style: TextStyle(color: Colors.white, fontSize: 32.sp),
            ),
            centerTitle: true,
            elevation: 0,
          ),
          body: GetBuilder<FontSaleOrderLogic>(builder: (logic) {
            return state.total != null
                ? Column(
                    children: [
                      Expanded(
                        child: ListView(
                            shrinkWrap: true,
                            primary: true,
                            children: [
                              Column(
                                children: [
                                  Container(
                                    padding: EdgeInsets.only(
                                        left: 30.w, right: 30.w, top: 20.w),
                                    decoration: BoxDecoration(
                                      color: Colors.white,
                                    ),
                                    child: Column(
                                      children: [
                                        Column(children: [
                                          state.orderAddress != null &&
                                      state.orderAddress["cont_empe"] != null
                                              ? GestureDetector(
                                                  onTap: () {
                                                    if (!state.isZiTi) {
                                                      Get.toNamed(
                                                          RouteConfig
                                                              .addressList,
                                                          arguments: {
                                                            "userNum":
                                                                state.userNum,
                                                            'adr_num_id': state
                                                                    .orderAddress[
                                                                'adr_num_id']
                                                          }).then((value) {
                                                        if (value != null &&
                                                            value["address"] !=
                                                                null) {
                                                          state.orderAddress =
                                                              value["address"];
                                                          logic.onReady();
                                                        }
                                                      });
                                                    } else {
                                                      toast("请关闭自提");
                                                    }
                                                  },
                                                  child: InkWell(
                                                      child: Row(
                                                    mainAxisAlignment:
                                                        MainAxisAlignment
                                                            .center,
                                                    crossAxisAlignment:
                                                        CrossAxisAlignment
                                                            .center,
                                                    children: [
                                                      Container(
                                                        width: 650.w,
                                                        child: Column(
                                                          mainAxisAlignment:
                                                              MainAxisAlignment
                                                                  .start,
                                                          crossAxisAlignment:
                                                              CrossAxisAlignment
                                                                  .start,
                                                          children: [
                                                            RichText(
                                                              text: TextSpan(
                                                                children: [
                                                                  TextSpan(
                                                                    text: state
                                                                        .orderAddress[
                                                                            "cont_empe"]
                                                                        .toString(),
                                                                    style: TextStyle(
                                                                        color: Colors
                                                                            .black,
                                                                        fontSize:
                                                                            28.w),
                                                                  ),
                                                                  WidgetSpan(
                                                                      child:
                                                                          SizedBox(
                                                                    width: 20.w,
                                                                  )),
                                                                  TextSpan(
                                                                    text: state
                                                                        .orderAddress[
                                                                            "contact_no"]
                                                                        .toString(),
                                                                    style: TextStyle(
                                                                        color: Colors
                                                                            .black,
                                                                        fontSize:
                                                                            28.sp),
                                                                  ),
                                                                  state.orderAddress[
                                                                              "default_sign"] ==
                                                                          1
                                                                      ? WidgetSpan(
                                                                          child: Container(
                                                                              margin: EdgeInsets.only(left: 10.w),
                                                                              padding: EdgeInsets.symmetric(horizontal: 5.w, vertical: 2.w),
                                                                              color: Config.mainColor,
                                                                              child: Text(
                                                                                "默认",
                                                                                style: TextStyle(
                                                                                  fontSize: 20.sp,
                                                                                  color: Colors.white,
                                                                                ),
                                                                              )),
                                                                        )
                                                                      : WidgetSpan(
                                                                          child:
                                                                              Container()),
                                                                ],
                                                              ),
                                                            ),
                                                            SizedBox(
                                                              height: 10.w,
                                                            ),
                                                            Text(
                                                              "${state.orderAddress["prv_name"]} ${state.orderAddress["city_name"]} ${state.orderAddress["city_area_name"]} ${state.orderAddress["town_name"] ?? ''} ${state.orderAddress["adr"]}",
                                                              style: TextStyle(
                                                                color: Colors
                                                                    .black,
                                                                fontSize: 26.sp,
                                                              ),
                                                              maxLines: 2,
                                                              textAlign:
                                                                  TextAlign
                                                                      .start,
                                                              overflow:
                                                                  TextOverflow
                                                                      .ellipsis,
                                                            ),
                                                            SizedBox(
                                                              height: 5.w,
                                                            ),

                                                          ],
                                                        ),
                                                      ),
                                                      SizedBox(
                                                        width: 5.w,
                                                      ),
                                                      Center(
                                                        child: Icon(
                                                          Icons
                                                              .arrow_forward_ios_rounded,
                                                          size: 15,
                                                          color:
                                                              Color(0xffB7B7B7),
                                                        ),
                                                      ),
                                                    ],
                                                  )),
                                                )
                                              : GestureDetector(
                                                  onTap: () {
                                                    Get.toNamed(
                                                        RouteConfig.addressList,
                                                        arguments: {
                                                          "userNum":
                                                              state.userNum,
                                                          'adr_num_id': state
                                                                      .orderAddress !=
                                                                  null
                                                              ? state.orderAddress[
                                                                  'adr_num_id']
                                                              : null
                                                        }).then((value) {
                                                      print("asd$value");
                                                      if (value["address"] !=
                                                          null) {
                                                        state.orderAddress =
                                                            value["address"];
                                                        state.isZiTi = false;
                                                        logic.onReady();
                                                      }
                                                    });
                                                  },
                                                  child: Container(
                                                    height: 50.w,
                                                    child: Text("请选择地址"),
                                                  )),

                                          state.isZiTi
                                              ? Row(
                                                  mainAxisAlignment:
                                                      MainAxisAlignment
                                                          .spaceBetween,
                                                  crossAxisAlignment:
                                                      CrossAxisAlignment.center,
                                                  children: [
                                                    Container(
                                                      width: 580.w,
                                                      child: Text(
                                                        state.adr,
                                                        style: TextStyle(
                                                          color:
                                                              Color(0xff333333),
                                                          fontSize: 26.sp,
                                                        ),
                                                        maxLines: 2,
                                                      ),
                                                    ),
                                                    Center(
                                                      child: GestureDetector(
                                                        onTap: () {
                                                          // Get.toNamed(
                                                          //     RouteConfig
                                                          //         .addressList);

                                                          showDialog(
                                                            context: context,
                                                            builder: (context) {
                                                              return _selectProduct(
                                                                  context);
                                                            },
                                                          );
                                                        },
                                                        child: Container(
                                                          width: 96.w,
                                                          height: 42.w,
                                                          decoration:
                                                              BoxDecoration(
                                                            borderRadius:
                                                                BorderRadius
                                                                    .all(Radius
                                                                        .circular(
                                                                            100)),
                                                            border: Border.all(
                                                              color: Color(
                                                                  0xffe1e1e1),
                                                            ),
                                                          ),
                                                          child: Center(
                                                            child: Text(
                                                              "修改",
                                                              style: TextStyle(
                                                                fontSize: 24.sp,
                                                                color: Color(
                                                                    0xff666666),
                                                              ),
                                                            ),
                                                          ),
                                                        ),
                                                      ),
                                                    )
                                                  ],
                                                )
                                              : Container(),
                                          SizedBox(
                                            height: 20.w,
                                          ),
                                        ]),
                                      ],
                                    ),
                                  ),
                                  Container(
                                    height: 24.w,
                                  ),

                                  /// 商品部分
                                  Container(
                                    child: Column(
                                      children: [
                                        ListView.builder(
                                          itemBuilder: (ctx, index) {
                                            return Container(
                                                color: Colors.white,
                                                padding: EdgeInsets.only(
                                                    left: 20.w, right: 20.w),
                                                child: Column(
                                                  children: [
                                                    Container(
                                                      padding: EdgeInsets.only(
                                                          top: 20.w,
                                                          bottom: 20.w),
                                                      decoration: BoxDecoration(
                                                        border: Border(
                                                          bottom: BorderSide(
                                                            color: Color(
                                                                0xfff0f0f0),
                                                          ),
                                                        ),
                                                      ),
                                                      child: Row(
                                                        crossAxisAlignment:
                                                            CrossAxisAlignment
                                                                .center,
                                                        mainAxisAlignment:
                                                            MainAxisAlignment
                                                                .start,
                                                        children: [
                                                          Image(
                                                            image: AssetImage(
                                                                "assets/images/orderStore.png"),
                                                            width: 36.w,
                                                          ),
                                                          SizedBox(
                                                            width: 5.w,
                                                          ),
                                                          Text(
                                                            logic.state.goodsItem[
                                                                    index][
                                                                "ec_shop_name"],
                                                            style: TextStyle(
                                                                fontSize: 26.sp,
                                                                color: Color(
                                                                    0xff333333)),
                                                          ),
                                                        ],
                                                      ),
                                                    ),
                                                    Container(
                                                      color: Colors.white,
                                                      padding:
                                                          EdgeInsets.symmetric(
                                                              vertical: 34.w),
                                                      child: ListView(
                                                        shrinkWrap: true,
                                                        //添加子元素
                                                        children: logic
                                                            .state
                                                            .goodsItem[index]
                                                                ["ec_packages"]
                                                                [0]
                                                                ["cartitemlist"]
                                                            .map<Widget>(
                                                                (item) {
                                                          return Row(
                                                            crossAxisAlignment:
                                                                CrossAxisAlignment
                                                                    .start,
                                                            children: [
                                                              Container(
                                                                  margin: EdgeInsets
                                                                      .only(
                                                                          right: 10
                                                                              .w),
                                                                  width: 96.w,
                                                                  height: 96.w,
                                                                  decoration:
                                                                      BoxDecoration(
                                                                    borderRadius:
                                                                        BorderRadius
                                                                            .all(
                                                                      Radius.circular(
                                                                          10.w),
                                                                    ),
                                                                    border:
                                                                        Border
                                                                            .all(
                                                                      color: Color(
                                                                          0xfff0f0f0),
                                                                    ),
                                                                  ),
                                                                  child:
                                                                      CachedImage(
                                                                    path: item[
                                                                        "image_default_id"],
                                                                    width:
                                                                        160.w,
                                                                    height:
                                                                        160.w,
                                                                  )),
                                                              Expanded(
                                                                child: Column(
                                                                  crossAxisAlignment:
                                                                      CrossAxisAlignment
                                                                          .start,
                                                                  children: [
                                                                    Text(
                                                                        item[
                                                                            'title'],
                                                                        overflow:
                                                                            TextOverflow
                                                                                .ellipsis,
                                                                        style: TextStyle(
                                                                            fontSize:
                                                                                13)),
                                                                    SizedBox(
                                                                        height:
                                                                            4),
                                                                    Row(
                                                                      mainAxisAlignment:
                                                                          MainAxisAlignment
                                                                              .spaceBetween,
                                                                      children: [
                                                                        Row(
                                                                          children: [
                                                                            Text(
                                                                              '￥',
                                                                              style: TextStyle(color: EsColors.Primary, fontSize: 12),
                                                                            ),
                                                                            Text(
                                                                              toConstraintLen(state.total["all_payment"], 3),
                                                                              style: TextStyle(color: EsColors.Primary),
                                                                            ),
                                                                            Text(
                                                                              ' x${logic.state.goodsItem[index]["ec_packages"][0]["cartitemlist"].length}',
                                                                              style: TextStyle(color: const Color(0xffcccccc)),
                                                                            ),
                                                                          ],
                                                                        ),

                                                                      ],
                                                                    ),
                                                                    Text(
                                                                        '发货时间:',
                                                                        style: TextStyle(
                                                                            fontSize:
                                                                                13)),
                                                                    state.dueTime !=
                                                                                '' &&
                                                                            state.dueTime !=
                                                                                null
                                                                        ? Container(
                                                                            child:
                                                                                Text(
                                                                              '${state.dueTime}',
                                                                              style: TextStyle(fontSize: 13),
                                                                              overflow: TextOverflow.ellipsis,
                                                                            ),
                                                                          )
                                                                        : Text(
                                                                            '暂无数据'),
                                                                  ],
                                                                ),
                                                              ),
                                                            ],
                                                          );
                                                        }).toList(),
                                                      ),
                                                    ),
                                                  ],
                                                ));
                                          },
                                          shrinkWrap: true,
                                          physics:
                                              NeverScrollableScrollPhysics(),
                                          itemCount:
                                              logic.state.goodsItem.length,
                                        ),
                                        Container(
                                          color: Colors.white,
                                          padding: EdgeInsets.only(
                                              left: 20.w,
                                              right: 20.w,
                                              bottom: 24.w),
                                          margin: EdgeInsets.only(bottom: 24.w),
                                          child: Row(
                                            children: [
                                              Container(
                                                margin: EdgeInsets.only(
                                                    right: 10.w),
                                                width: 96.w,
                                                height: 96.w,
                                              ),
                                              Expanded(
                                                child: Column(
                                                  crossAxisAlignment:
                                                      CrossAxisAlignment.start,
                                                  children: [
                                                    Text(
                                                      '预售价格￥${toConstraintLen(state.depositAmount + state.balanceAmount, 3)}, 付定金￥${toConstraintLen(state.depositAmount, 3)}, 尾款需要￥${toConstraintLen(state.balanceAmount, 3)}',
                                                      style: TextStyle(
                                                          color: const Color(
                                                              0xff666666),
                                                          fontSize: 12),
                                                    ),
                                                    Text(
                                                        '           (如有优惠券可在尾款使用)',
                                                        style: TextStyle(
                                                            color: const Color(
                                                                0xff666666),
                                                            fontSize: 12)),
                                                    SizedBox(height: 20),
                                                    Row(
                                                      mainAxisAlignment:
                                                          MainAxisAlignment
                                                              .spaceBetween,
                                                      children: [
                                                        Column(
                                                          children: [
                                                            Text(
                                                              '定金',
                                                              style: TextStyle(
                                                                  color: EsColors
                                                                      .Primary,
                                                                  fontSize: 13),
                                                            ),
                                                            Container(
                                                                width: 1,
                                                                height: 25,
                                                                color: const Color(
                                                                    0xff333333)),
                                                            Text(
                                                              '尾款',
                                                              style: TextStyle(
                                                                  fontSize: 13),
                                                            ),
                                                          ],
                                                        ),
                                                        Column(
                                                          children: [
                                                            Text(
                                                              '￥${toConstraintLen(state.depositAmount, 3)}',
                                                              style: TextStyle(
                                                                  color: EsColors
                                                                      .Primary,
                                                                  fontSize: 13),
                                                            ),
                                                            Container(
                                                                width: 1,
                                                                height: 25),
                                                            Text(
                                                              '￥${toConstraintLen(state.balanceAmount, 3)}',
                                                              style: TextStyle(
                                                                  fontSize: 13),
                                                            ),
                                                          ],
                                                        ),
                                                      ],
                                                    ),
                                                  ],
                                                ),
                                              ),
                                            ],
                                          ),
                                        ),
                                      ],
                                    ),
                                  ),
                                  Column(
                                    crossAxisAlignment:
                                        CrossAxisAlignment.start,
                                    children: [
                                      /// 缺货提示
                                      TextInputWidget(
                                        title: "如遇缺货",
                                        suffixWidget: GestureDetector(
                                          onTap: () {
                                            if (state.type != 3) {
                                              _changeRemark(context);
                                            }
                                          },
                                          child: Row(
                                            mainAxisAlignment:
                                                MainAxisAlignment.end,
                                            children: [
                                              Text(
                                                state.remarkList[
                                                    state.remarkType]['remark'],
                                                style: TextStyle(
                                                  fontSize: 28.sp,
                                                  color: Color(0xff333333),
                                                ),
                                              ),
                                              Icon(
                                                Icons.arrow_forward_ios_rounded,
                                                color: Color(0xffcccccc),
                                                size: 14,
                                              )
                                            ],
                                          ),
                                        ),
                                      ),


                                      if (state.type != 3 &&
                                          state.total["all_payment"]
                                                  .toString() !=
                                              '0.0')


                                        /// 订金金额
                                        if (state.paytype == 2 &&
                                            state.total["all_payment"]
                                                    .toString() !=
                                                '0.0')
                                          Container(
                                            color: Colors.white,
                                            padding: EdgeInsets.symmetric(
                                                horizontal: 20.w),
                                            child: Column(
                                              crossAxisAlignment:
                                                  CrossAxisAlignment.center,
                                              mainAxisAlignment:
                                                  MainAxisAlignment.start,
                                              children: [
                                                Row(
                                                  children: [

                                                  ],
                                                ),
                                                SizedBox(
                                                  width: 750.w,
                                                  height: 1,
                                                  child: DecoratedBox(
                                                    decoration: BoxDecoration(
                                                        color:
                                                            Color(0xffefefef)),
                                                  ),
                                                ),
                                              ],
                                            ),
                                          ),

                                      /// 尾款金额
                                      if (state.paytype == 2 &&
                                          state.total["all_payment"]
                                                  .toString() !=
                                              '0.0')
                                        Container(
                                          color: Colors.white,
                                          padding: EdgeInsets.symmetric(
                                              vertical: 20.w, horizontal: 20.w),
                                          child: Column(
                                            crossAxisAlignment:
                                                CrossAxisAlignment.center,
                                            mainAxisAlignment:
                                                MainAxisAlignment.start,
                                            children: [
                                              Row(
                                                mainAxisAlignment:
                                                    MainAxisAlignment
                                                        .spaceBetween,
                                                children: [
                                                  Text(
                                                    '尾款金额',
                                                    style: TextStyle(
                                                      color: Color(0xff666666),
                                                      fontSize: 28.w,
                                                    ),
                                                  ),
                                                  SizedBox(
                                                    width: 30.w,
                                                  ),
                                                  Row(
                                                    children: [
                                                      Text(
                                                        '${state.endMoney != null ? state.endMoney : '自动带出'}',
                                                        style: TextStyle(
                                                          fontSize: 28.sp,
                                                          color: state.endMoney !=
                                                                  null
                                                              ? Color(
                                                                  0xff000000)
                                                              : Color(
                                                                  0xffcccccc),
                                                        ),
                                                      ),
                                                      if (state.endMoney !=
                                                          null)
                                                        Text(
                                                          '元',
                                                          style: TextStyle(
                                                            fontSize: 28.sp,
                                                            color: state.endMoney !=
                                                                    null
                                                                ? Color(
                                                                    0xff000000)
                                                                : Color(
                                                                    0xffcccccc),
                                                          ),
                                                        )
                                                    ],
                                                  ),
                                                ],
                                              ),
                                              SizedBox(
                                                height: 15.w,
                                              ),
                                              SizedBox(
                                                width: 750.w,
                                                height: 1,
                                                child: DecoratedBox(
                                                  decoration: BoxDecoration(
                                                      color: Color(0xffefefef)),
                                                ),
                                              ),
                                            ],
                                          ),
                                        ),

                                      /// 尾款预计付款日期
                                      if (state.paytype == 2 &&
                                          state.total["all_payment"]
                                                  .toString() !=
                                              '0.0')
                                        InkWell(
                                          onTap: () {
                                            DatePicker.showDatePicker(context,
                                                showTitleActions: true,
                                                minTime: DateTime(2010, 1, 1),
                                                maxTime: DateTime(2022, 12, 31),
                                                onConfirm: (date) {
                                              logic.selectDate(date);
                                            },
                                                currentTime: DateTime.now(),
                                                locale: LocaleType.zh);
                                          },
                                          child: Container(
                                            color: Colors.white,
                                            padding: EdgeInsets.only(
                                              left: 20.w,
                                              right: 20.w,
                                              bottom: 20.w,
                                            ),
                                            child: Column(
                                              crossAxisAlignment:
                                                  CrossAxisAlignment.center,
                                              mainAxisAlignment:
                                                  MainAxisAlignment.start,
                                              children: [
                                                Row(
                                                  mainAxisAlignment:
                                                      MainAxisAlignment
                                                          .spaceBetween,
                                                  children: [
                                                    Text(
                                                      '尾款预计付款日期',
                                                      style: TextStyle(
                                                        color:
                                                            Color(0xff666666),
                                                        fontSize: 28.w,
                                                      ),
                                                    ),
                                                    SizedBox(
                                                      width: 30.w,
                                                    ),
                                                    Row(
                                                      mainAxisAlignment:
                                                          MainAxisAlignment.end,
                                                      children: [
                                                        Text(
                                                          '${state.time1 != '' ? state.time1 : '请选择日期'}',
                                                          style: TextStyle(
                                                            fontSize: 28.sp,
                                                            color: state.time1 !=
                                                                    ''
                                                                ? Color(
                                                                    0xff000000)
                                                                : Color(
                                                                    0xffcccccc),
                                                          ),
                                                        ),
                                                        Icon(
                                                          Icons
                                                              .arrow_forward_ios_rounded,
                                                          color:
                                                              Color(0xffcccccc),
                                                          size: 14,
                                                        )
                                                      ],
                                                    )
                                                  ],
                                                ),
                                                SizedBox(
                                                  height: 15.w,
                                                ),
                                                SizedBox(
                                                  width: 750.w,
                                                  height: 1,
                                                  child: DecoratedBox(
                                                    decoration: BoxDecoration(
                                                        color:
                                                            Color(0xffefefef)),
                                                  ),
                                                ),
                                              ],
                                            ),
                                          ),
                                        ),

                                      // 订单备注
                                      Container(
                                        color: Colors.white,
                                        child: Padding(
                                          padding: EdgeInsets.symmetric(
                                            horizontal: 20.w,
                                          ),
                                          child: Container(
                                            decoration: BoxDecoration(
                                              color: Colors.white,
                                              borderRadius: BorderRadius.all(
                                                Radius.circular(20.w),
                                              ),
                                              border: Border.all(
                                                color: Color(0xfff0f0f0),
                                              ),
                                            ),
                                            padding: EdgeInsets.symmetric(
                                                horizontal: 20.w),
                                            child: TextField(
                                              controller: state.controller,
                                              style: TextStyle(fontSize: 24.sp),
                                              maxLines: 5,
                                              decoration: InputDecoration(
                                                focusedBorder: InputBorder.none,
                                                enabledBorder: InputBorder.none,
                                                hintText: '添加订单备注',
                                                hintStyle:
                                                    TextStyle(fontSize: 24.sp),
                                              ),
                                            ),
                                          ),
                                        ),
                                      ),
                                      Container(
                                        color: Colors.white,
                                        height: 24.w,
                                      ),
                                    ],
                                  ),
                                  Container(
                                    height: 24.w,
                                  ),
                                  Container(
                                    color: Colors.white,
                                    padding: EdgeInsets.all(20.w),
                                    child: Column(
                                      children: [
                                        Row(
                                          crossAxisAlignment:
                                              CrossAxisAlignment.center,
                                          mainAxisAlignment:
                                              MainAxisAlignment.spaceBetween,
                                          children: [
                                            Text(
                                              "商品定金",
                                              style: TextStyle(
                                                color: Color(0xff666666),
                                                fontSize: 28.sp,
                                              ),
                                            ),
                                            Text(
                                              toConstraintLen(
                                                  state.total["all_fee"], 3),
                                              style: TextStyle(
                                                color: Color(0xff222222),
                                                fontSize: 28.sp,
                                              ),
                                            ),
                                          ],
                                        ),
                                        SizedBox(
                                          height: 20.w,
                                        ),
                                        Row(
                                          crossAxisAlignment:
                                              CrossAxisAlignment.center,
                                          mainAxisAlignment:
                                              MainAxisAlignment.spaceBetween,
                                          children: [
                                            Text(
                                              "配送运费",
                                              style: TextStyle(
                                                color: Color(0xff666666),
                                                fontSize: 28.sp,
                                              ),
                                            ),
                                            Text(
                                              toConstraintLen(
                                                  state.total["actual_freight"],
                                                  3),
                                              style: TextStyle(
                                                color: Color(0xff222222),
                                                fontSize: 28.sp,
                                              ),
                                            ),
                                          ],
                                        ),
                                      ],
                                    ),
                                  ),
                                  Container(
                                    height: 24.w,
                                  ),
                                ],
                              )
                            ]),
                      ),
                      Container(
                        color: Colors.white,
                        width: 750.w,
                        height: 100.w,
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          crossAxisAlignment: CrossAxisAlignment.center,
                          children: [
                            Row(
                              crossAxisAlignment: CrossAxisAlignment.center,
                              children: [
                                SizedBox(
                                  width: 10.w,
                                ),
                                Text(
                                  '￥${toConstraintLen(state.total["all_payment"], 3)}',
                                  style: TextStyle(
                                      color: Config.mainColor, fontSize: 32.w),
                                ),
                                SizedBox(
                                  width: 20.w,
                                ),
                                Column(
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  mainAxisAlignment: MainAxisAlignment.center,
                                  children: [

                                  ],
                                ),
                              ],
                            ),
                            SizedBox(
                              width: 10.w,
                            ),
                            GestureDetector(
                              onTap: () {
                                // logic.orderSubmit();
                                if (state.isJiFen) {
                                  logic.orderSubmit();
                                  return;
                                } else {
                                  // if (state.total["all_payment"] <= 0) {
                                  //   logic.orderSubmit();
                                  //   return;
                                  // } else {
                                  if (state.GiftcardList.length <= 0) {
                                    logic.orderSubmit();
                                    return;
                                  }
                                  payBottomSheet.showPay(context, state,
                                      logic.orderSubmit, logic.clickGift);
                                }
                              },
                              child: Container(
                                color: Config.mainColor,
                                height: 100.w,
                                width: 340.w,
                                child: Center(
                                  child: Text(
                                    "提交订单",
                                    style: TextStyle(
                                      color: Colors.white,
                                      fontSize: 36.sp,
                                    ),
                                  ),
                                ),
                              ),
                            )
                          ],
                        ),
                      ),
                    ],
                  )
                : Container();
          })),
    );
  }

  /// 如遇缺货
  _changeRemark(context) async {
    var result = await showCupertinoModalPopup(
        context: context,
        builder: (context) {
          return CupertinoActionSheet(
            title: Text('如遇缺货'),
            actions: List.generate(logic.state.remarkList.length, (index) {
              Map remark = logic.state.remarkList[index];
              return CupertinoActionSheetAction(
                child: Container(
                  width: 500.w,
                  child: Stack(
                    children: [
                      Center(
                        child: Text(remark['remark']),
                      ),
                      logic.state.remarkType == index
                          ? Positioned(
                              right: 0.w,
                              child: Icon(
                                Icons.check,
                                color: Colors.blue,
                                size: 48.sp,
                              ),
                            )
                          : SizedBox(),
                    ],
                  ),
                ),
                onPressed: () {
                  logic.state.remarkType = index;
                  logic.update();
                  Navigator.of(context).pop('$index');
                },
                isDefaultAction: true,
              );
            }),

            cancelButton: CupertinoActionSheetAction(
              child: Text('取消'),
              onPressed: () {
                Navigator.of(context).pop('cancel');
              },
            ),
          );
        });
  }

  void selectProduct(BuildContext context) {
    //用于在底部打开弹框的效果
    showModalBottomSheet(
        isScrollControlled: true,
        backgroundColor: Colors.white,
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(10.0.w),
        ),
        builder: (BuildContext context) {
          //构建弹框中的内容
          return Container(
              height: 1000.w, width: 750.w, child: FSActiveCouponItem());
        },
        context: context);
  }

  /// 自提点
  _selectProduct(context) {
    //打开弹框的效果
    return SimpleDialog(
      title: Text(
        '选择自提点',
        style: TextStyle(
          color: Color(0xff333333),
          fontSize: 26.w,
        ),
        textAlign: TextAlign.center,
      ),
      titleTextStyle: TextStyle(
          fontSize: 16, fontWeight: FontWeight.bold, color: Colors.blue),
      children: [
        GetBuilder<FontSaleOrderLogic>(
          builder: (logic) {
            return Container(
              height: 400.w,
              child: Container(
                padding: EdgeInsets.symmetric(
                  horizontal: 20.w,
                ),
                height: 400.w, // Change as per your requirement
                width: 300.w, // Ch
                child: ListView.builder(
                  primary: true,
                  shrinkWrap: true,
                  itemBuilder: (ctx, i) {
                    var item = state.selfHelpAddressList[i];
                    return GestureDetector(
                      onTap: () {
                        logic.selectDian(i, item);
                      },
                      child: Container(
                        padding: EdgeInsets.only(bottom: 15.w),
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.start,
                          crossAxisAlignment: CrossAxisAlignment.center,
                          children: [
                            state.selected == i
                                ? Image(
                                    image: AssetImage(
                                        "assets/images/selectIcon1.png"),
                                    width: 36.w,
                                  )
                                : Image(
                                    image: AssetImage("assets/images/save.png"),
                                    width: 36.w,
                                  ),
                            SizedBox(
                              width: 10.w,
                            ),
                            Expanded(
                              child: Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: [
                                  Text(
                                    item["sub_unit_name"],
                                    style: TextStyle(
                                      color: Color(0xff222222),
                                      fontSize: 28.sp,
                                    ),
                                  ),
                                  Text(
                                    '${item["prv_name"]}${item["city_name"]}${item["city_area_name"]}${item["town_name"] ?? ''}${item["adr"]}',
                                    style: TextStyle(
                                      color: Color(0xff999999),
                                      fontSize: 24.sp,
                                    ),
                                    maxLines: 2,
                                    textAlign: TextAlign.start,
                                    overflow: TextOverflow.ellipsis,
                                  ),
                                ],
                              ),
                            ),
                            Container(
                              padding: EdgeInsets.only(top: 10.w, bottom: 10.w),
                              child: Divider(
                                color: Color(0xff000000),
                              ),
                            ),
                          ],
                        ),
                      ),
                    );
                  },
                  itemCount: state.selfHelpAddressList?.length ?? 0,
                ),
              ),
            );
          },
        ),
        SimpleDialogOption(
          onPressed: () {
            Navigator.pop(context);
            logic.submitDian();
          },
          child: Container(
            color: Colors.white,
            child: Container(
              height: 80.w,
              decoration: BoxDecoration(
                color: Config.mainColor,
                borderRadius: BorderRadius.circular(40.w),
              ),
              child: Center(
                child: Text(
                  "确定",
                  style: TextStyle(
                    color: Colors.white,
                    fontSize: 28.w,
                  ),
                ),
              ),
            ),
          ),
        ),
      ],
      backgroundColor: Colors.white,
      elevation: 10,
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(5),
      ),
    );
  }
}

///支付
showPayWay(BuildContext context, ClickCallback clickCallback, state, loice) {
  int index = 0;
}

class FSActiveCouponItem extends StatefulWidget {
  FSActiveCouponItem({Key key}) : super(key: key);

  @override
  _FSActiveCouponItemState createState() {
    return _FSActiveCouponItemState();
  }
}

class _FSActiveCouponItemState extends State<FSActiveCouponItem>
    with SingleTickerProviderStateMixin {
  TabController tabControllers;

  @override
  void initState() {
    tabControllers = TabController(length: 2, vsync: this);
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Column(children: [
      TabBar(controller: tabControllers, labelColor: Colors.black, tabs: [
        Tab(text: '可用优惠券'),
        Tab(text: '不可用优惠券'),
      ]),
      Expanded(
        child: TabBarView(
            controller: tabControllers,
            children: [FSAvailableRoll(), FSnoavailableRollState()]),
      )
    ]);
  }
}

class FSAvailableRoll extends StatefulWidget {
  FSAvailableRoll({Key key}) : super(key: key);

  @override
  FSAvailableRollState createState() => FSAvailableRollState();
}

class FSAvailableRollState extends State<FSAvailableRoll> {
  final FontSaleOrderLogic logic = Get.put(FontSaleOrderLogic());
  final FontSaleOrderState state = Get.find<FontSaleOrderLogic>().state;

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        GetBuilder<FontSaleOrderLogic>(builder: (logic) {
          return Expanded(
            child: Container(
              padding: EdgeInsets.symmetric(vertical: 20.w),
              margin: EdgeInsets.only(right: 22.w, left: 22.w),
              decoration: BoxDecoration(
                color: Colors.white,
                border: Border(
                  bottom: BorderSide(
                    color: Color(0xfff0f0f0),
                  ),
                ),
              ),
              child: SingleChildScrollView(
                child: GetBuilder<FontSaleOrderLogic>(builder: (logic) {
                  return ListView.builder(
                    itemBuilder: (ctx, index) {
                      var item = state.couponList[index];
                      print(item);
                      return InkWell(
                        onTap: () {
                          Navigator.pop(context);
                          logic.selectCoupon(index, item);
                        },
                        child: Container(
                          width: 718.w,
                          height: 208.w,
                          margin: EdgeInsets.symmetric(vertical: 10.w),
                          decoration: BoxDecoration(
                            borderRadius: BorderRadius.all(
                              Radius.circular(16.w),
                            ),
                            image: DecorationImage(
                              image: AssetImage("assets/images/couponbg.png"),
                              fit: BoxFit.cover,
                            ),
                            boxShadow: [
                              BoxShadow(
                                color: Color(0xFF6498F1).withOpacity(.14),
                                offset: Offset(0, 0), //阴影y轴偏移量
                                blurRadius: 1,
                                spreadRadius: .3,
                              )
                            ],
                          ),
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              Container(
                                width: 200.w,
                                padding: EdgeInsets.only(top: 50.w),
                                child: Column(
                                  children: [
                                    RichText(
                                      text: TextSpan(
                                        children: [
                                          TextSpan(
                                            text: '￥',
                                            style: TextStyle(
                                              color: Color(0xffC99F4F),
                                              fontSize: 28.w,
                                            ),
                                          ),
                                          TextSpan(
                                            text: item['qmz'].toString(),
                                            style: TextStyle(
                                              color: Color(0xffC99F4F),
                                              fontSize: 50.w,
                                            ),
                                          ),
                                        ],
                                      ),
                                    ),
                                    Text(
                                      "满${item['least_cost'].toString()}元可用",
                                      style: TextStyle(
                                        color: Color(0xffC99F4F),
                                        fontSize: 22.w,
                                      ),
                                    )
                                  ],
                                ),
                              ),
                              SizedBox(width: 10.w),
                              Container(
                                width: 310.w,
                                child: Column(
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  mainAxisAlignment: MainAxisAlignment.center,
                                  children: [
                                    Text(
                                      item['qname'].toString(),
                                      style: TextStyle(
                                        color: Color(0xff000000),
                                        fontSize: 28.sp,
                                      ),
                                      maxLines: 1,
                                    ),
                                    SizedBox(height: 60.w),
                                    Text(
                                      "${item['sdate'].split(" ")[0]}-${item['edate'].split(" ")[0]}",
                                      style: TextStyle(
                                        color: Color(0xff999999),
                                        fontSize: 24.sp,
                                      ),
                                    ),
                                  ],
                                ),
                              ),
                              SizedBox(width: 20.w),
                              Container(
                                padding: EdgeInsets.only(right: 10.w),
                                child: Image.asset(
                                  item['show_status'] == 1
                                      ? "assets/images/select2.png"
                                      : "assets/images/select1.png",
                                  width: 35.w,
                                ),
                              )
                            ],
                          ),
                        ),
                      );
                    },
                    itemCount: state.couponList.length,
                    shrinkWrap: true,
                    physics: NeverScrollableScrollPhysics(),
                  );
                }),
              ),
            ),
          );
        }),
      ],
    );
  }
}

class FSnoavailableRollState extends StatefulWidget {
  FSnoavailableRollState({Key key}) : super(key: key);

  @override
  _FSnoavailableRollStateState createState() => _FSnoavailableRollStateState();
}

class _FSnoavailableRollStateState extends State<FSnoavailableRollState> {
  final FontSaleOrderLogic logic = Get.put(FontSaleOrderLogic());
  final FontSaleOrderState state = Get.find<FontSaleOrderLogic>().state;

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Expanded(
          child: Container(
            padding: EdgeInsets.symmetric(vertical: 20.w),
            margin: EdgeInsets.only(right: 22.w, left: 22.w),
            decoration: BoxDecoration(
              color: Colors.white,
              border: Border(
                bottom: BorderSide(
                  color: Color(0xfff0f0f0),
                ),
              ),
            ),
            child: SingleChildScrollView(
              child: GetBuilder<FontSaleOrderLogic>(builder: (logic) {
                return ListView.builder(
                  itemBuilder: (ctx, index) {
                    var item = state.nocouponList[index];
                    print(item);
                    return Container(
                      width: 718.w,
                      height: 208.w,
                      margin: EdgeInsets.symmetric(vertical: 10.w),
                      decoration: BoxDecoration(
                        borderRadius: BorderRadius.all(
                          Radius.circular(16.w),
                        ),
                        image: DecorationImage(
                          image: AssetImage("assets/images/couponbg.png"),
                          fit: BoxFit.cover,
                        ),
                        boxShadow: [
                          BoxShadow(
                            color: Color(0xFF6498F1).withOpacity(.14),
                            offset: Offset(0, 0), //阴影y轴偏移量
                            blurRadius: 1,
                            spreadRadius: .3,
                          )
                        ],
                      ),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.start,
                        children: [
                          Container(
                            width: 200.w,
                            padding: EdgeInsets.only(top: 50.w),
                            child: Column(
                              children: [
                                RichText(
                                  text: TextSpan(
                                    children: [
                                      TextSpan(
                                        text: '￥',
                                        style: TextStyle(
                                          color: Color(0xffC99F4F),
                                          fontSize: 28.w,
                                        ),
                                      ),
                                      TextSpan(
                                        text: item['qmz'].toString(),
                                        style: TextStyle(
                                          color: Color(0xffC99F4F),
                                          fontSize: 50.w,
                                        ),
                                      ),
                                    ],
                                  ),
                                ),
                                Text(
                                  "满${item['least_cost'].toString()}元可用",
                                  style: TextStyle(
                                    color: Color(0xffC99F4F),
                                    fontSize: 22.w,
                                  ),
                                )
                              ],
                            ),
                          ),
                          SizedBox(width: 30.w),
                          Container(
                            width: 310.w,
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              mainAxisAlignment: MainAxisAlignment.center,
                              children: [
                                Text(
                                  item['qname'].toString(),
                                  style: TextStyle(
                                    color: Color(0xff000000),
                                    fontSize: 28.sp,
                                  ),
                                  maxLines: 1,
                                ),
                                SizedBox(height: 60.w),
                                Text(
                                  "${item['sdate'].split(" ")[0]}-${item['edate'].split(" ")[0]}",
                                  style: TextStyle(
                                    color: Color(0xff999999),
                                    fontSize: 24.sp,
                                  ),
                                ),
                                // SizedBox(height: 20.w),
                                // Text(
                                //   "剩余990张",
                                //   style: TextStyle(
                                //     color: Color(0xff666666),
                                //     fontSize: 24.sp,
                                //   ),
                                // ),
                              ],
                            ),
                          ),
                        ],
                      ),
                    );
                  },
                  itemCount: state.nocouponList.length,
                  shrinkWrap: true,
                  physics: NeverScrollableScrollPhysics(),
                );
              }),
            ),
          ),
        ),
      ],
    );
  }
}
